﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Print_UI
{
    public partial class FrmStatistcSchooFee : CommonForm
    {
        public FrmStatistcSchooFee()
        {
            InitializeComponent();
        }

        private void FrmStatistcSchooFee_Load(object sender, EventArgs e)
        {
            LoadDepartment();
            LoadData();
            LoadSector();



        }
        private void LoadDepartment()
        {
            try
            {
                var departmentAdmin = from obj in Model.CAT_Department.OrderBy(c => c.Name)
                                      select obj;
                Dictionary<int, string> dicAdmin = new Dictionary<int, string>();
                dicAdmin.Add(-1, "----Chọn Khoa----");
                foreach (var item in departmentAdmin)
                {
                    dicAdmin.Add(item.ID, item.Name);

                }
                cboDepartment.Properties.ValueMember = "Key";
                cboDepartment.Properties.DisplayMember = "Value";
                cboDepartment.Properties.DataSource = dicAdmin;
                cboDepartment.EditValue = -1;

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void LoadSector()
        {
            try
            {
                int iDepartment = Convert.ToInt32(cboDepartment.EditValue);
                var Query = from obj in Model.CAT_Sector.Where(c => c.DepartmentID == iDepartment)
                            select obj;
                Dictionary<int, string> dic = new Dictionary<int, string>();
                dic.Add(-1, "----Chọn Ngành----");
                foreach (var item in Query)
                {
                    dic.Add(item.ID, item.Name);

                }
                cboSector.Properties.ValueMember = "Key";
                cboSector.Properties.DisplayMember = "Value";
                cboSector.Properties.DataSource = dic;
                cboSector.EditValue = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void LoadData()
        {
            int iDepart = Convert.ToInt32(cboDepartment.EditValue);
             int iSector = Convert.ToInt32(cboSector.EditValue);
             DataTable dt = new DataTable();
             dt.Columns.Add("Khoa",typeof(string));
             dt.Columns.Add("Year", typeof(string));
             dt.Columns.Add("Semester", typeof(string));
             dt.Columns.Add("Fee", typeof(decimal));
            
          foreach(var item in Model.CAT_Year)
          {
              foreach (var iy in Model.CAT_Year)
              {
                  foreach (var isemester in Model.CAT_Semester)
                  {
                      var obj = Model.PRI_StudentInSemester.Where(c => c.DepartmentID == iDepart && c.PRI_Student.YearID == item.ID && c.YearID == iy.ID && c.SemesterID == isemester.ID && c.CAT_Class.SectorID == iSector).FirstOrDefault();
                      if (obj != null)
                      {
                          dt.Rows.Add(item.Course, iy.Name,isemester.SemesterName,obj.SchoolFee);
                      }
                  }
              }
          }
          gvdata.DataSource = dt;
          gridColumn3.DisplayFormat.FormatString = "{0:#,###.##}";
          gridColumn3.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
        }
        private void cboDepartment_EditValueChanged(object sender, EventArgs e)
        {
            LoadSector();

        }

        private void cboSector_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnCancle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if(MessageBox.Show("Thoát khỏi giao diện này","Thông Báo",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
            {
                this.Close();

            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshModel();
            LoadData();
        }

    }
}
